﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;


namespace WindowsGame1
{
    class Sprite
    {
        Texture2D texture;
        Point frameSize;
        Point frameSheetSize;
        Point currentFrame;
        double frameMillsSecond;
        double lastFrameMillsSecond = 0;

        public Sprite(string textureString, Point frameSize, Point frameSheetSize, Point currentFrame, double frameMillsSecond)
        {
            ResourceManager.Instance.GetResource(textureString, out this.texture);
            this.frameSize = frameSize;
            this.frameSheetSize = frameSheetSize;
            this.currentFrame = currentFrame;
            this.frameMillsSecond = frameMillsSecond;
        }

        public void Update(GameTime gameTime)
        {
            lastFrameMillsSecond += gameTime.ElapsedGameTime.Milliseconds;
            if (lastFrameMillsSecond > frameMillsSecond)
            {
                lastFrameMillsSecond -= frameMillsSecond;
                ++currentFrame.X;
                if (currentFrame.X >= frameSheetSize.X)
                {
                    currentFrame.X = 0;
                    ++currentFrame.Y;
                }
                if (currentFrame.Y >= frameSheetSize.Y)
                {
                    currentFrame = Point.Zero;
                }
            }
            
        }

        public void Draw(SpriteBatch spriteBatch, Vector2 position)
        {
            spriteBatch.Draw(texture, position,
                new Rectangle(currentFrame.X * frameSize.X, currentFrame.Y * frameSize.Y, frameSize.X, frameSize.Y),
                Color.White, 0f, Vector2.Zero, 1.5f, SpriteEffects.None, 0f);
        }
    }
}
